package com.atomic.test.example.controller;

import com.atomic.test.example.common.ErrorCode;
import com.atomic.test.example.common.Result;
import com.atomic.test.example.entity.Topic;
import com.atomic.test.example.exception.ExceptionCode;
import com.atomic.test.example.service.ITopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.atomic.test.example.utils.CheckDataUtil.createTopicCheck;


/**
 * Created by dreamyao on 2017/3/6.
 */
@Controller
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private ITopicService topicService;

    /**
     * 创建话题
     * @param topic 话题信息
     * @return 返回创建话题的数量
     */
    @ResponseBody
    @RequestMapping("/createTopic")
    public Result<Integer> createTopic(@RequestBody Topic topic) {
        if (!createTopicCheck(topic)) {
            return new Result<>(ExceptionCode.ARGUMENT_ILLEGAL, "请求参数不合法！");
        }
        Integer num = topicService.createTopic(topic);
        return new Result<>(num);
    }

    /**
     * 更新话题
     * @param topic 话题信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateTopic")
    public Result<Void> updateTopic(@RequestBody Topic topic) {
        if (!createTopicCheck(topic) && topic.getId() == null) {
            return new Result<>(ExceptionCode.ARGUMENT_ILLEGAL, "请求参数不合法！");
        }
        topicService.updateTopic(topic);
        return new Result<>();
    }

    /**
     * 根据话题Id查询话题信息
     * @param topicId 话题Id
     * @return 话题信息
     */
    @ResponseBody
    @RequestMapping("/findTopic")
    public Result<Topic> findOneTopicById(Integer topicId) {
        if (topicId == null || topicId.intValue() <= 0) {
            return new Result<>(ExceptionCode.ARGUMENT_ILLEGAL, "请求参数不合法！");
        }
        Topic queryTopic = topicService.findOneTopicById(topicId);
        return new Result<>(queryTopic);
    }

    /**
     * 查询所有话题，支持分页功能
     * @return
     */
    @ResponseBody
    @RequestMapping("/findAllTopic")
    public Result<List<Topic>> findAllTopic(){
        List<Topic> topicQueryPageList=topicService.findAllTopic();
        return new Result<>(topicQueryPageList);
    }

    /**
     * 根据话题标题查询话题信息
     * @param topicTitle
     * @return
     */
    @ResponseBody
    @RequestMapping("/findTopicByTitle")
    public Result<Topic> findTopicByTopicTitle(String topicTitle) {
        if (topicTitle == null || topicTitle.equals("")) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        Topic topic = topicService.findTopicByTopicTitle(topicTitle);
        return new Result<>(topic);
    }

    /**
     * 根据话题标题删除话题
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteTopicByTitle")
    public Result<Void> deleteTopicByTopicTitle(String topicTitle) {
        if (topicTitle == null || topicTitle.equals("") || topicTitle.toCharArray().length > 50) {
            return new Result<>(ErrorCode.PARAMETER_ERROR, "参数错误！");
        }
        topicService.deleteTopicByTopicTitle(topicTitle);
        return new Result<>();
    }
}
